//
//  SCLoginButton.m
//  SoocQiPei
//
//  Created by 段帅 on 2017/4/10.
//  Copyright © 2017年 Sooc. All rights reserved.
//

#import "SCLoginButton.h"
#import "SCUIToolHeader.h"


#define Color_title [UIColor whiteColor]
#define Color_back_ban [UIColor colorWithRed:211.0/255 green:211.0/255 blue:211.0/255 alpha:1.0f]
#define Color_back_normal  [UIColor colorWithRed:103/255.0 green:58/255.0 blue:183/255.0 alpha:1.0f]


#define Color_line_High [UIColor colorWithRed:219.0/255 green:219.0/255 blue:219.0/255 alpha:1.0f]
#define Color_line_normal [UIColor colorWithRed:103/255.0 green:58/255.0 blue:183/255.0 alpha:1.0f]


@implementation SCLoginButton
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    
    return self;
    
}


-(void)createUI{
    
    
    
    
    
    
    
    [self setTitleColor:Color_title forState:UIControlStateNormal];
    
    
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    [self setBackgroundImage:[self imageWithColor:Color_Main] forState:UIControlStateNormal];
    
    [self setBackgroundImage:[self imageWithColor:Color_back_high] forState:UIControlStateHighlighted];
    [self setBackgroundImage:[self imageWithColor:Color_back_ban] forState:UIControlStateSelected];
    
    self.layer.cornerRadius = 2.5;
    self.layer.masksToBounds = YES;
    
    
    self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.42].CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 5);
    self.layer.shadowOpacity = 1 ;
    
    //    self.clipsToBounds = false ;
    //    
    
}

-(void)setIsCanUse:(BOOL )isCanUse{
    
    
    if (isCanUse == NO) {
        self.selected = YES;
        self.userInteractionEnabled = NO;
        self.layer.borderColor = Color_line_High.CGColor;
    }else{
        
        self.selected = NO;
        
        self.userInteractionEnabled = YES;
         self.layer.borderColor = Color_line_normal.CGColor;
        
    }
    
    
    
    
}




- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha  image:(UIImage*)image

{
    
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    
    
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    
    
    
    CGContextScaleCTM(ctx, 1, -1);
    
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    
    
    CGContextSetAlpha(ctx, alpha);
    
    
    
    CGContextDrawImage(ctx, area, image.CGImage);
    
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    UIGraphicsEndImageContext();
    
    
    
    return newImage;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
